﻿/*
 * Copyright (C) 2023 - 2024 lchfj.cn
 * 
 * This file is part of FastestHost.
 * 
 * FastestHost is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * FastestHost is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with FastestHost. If not, see <https://www.gnu.org/licenses/>.
 */

using Cn.Lchfj.FastestHost.Network;

namespace Cn.Lchfj.FastestHost
{
    internal class RunNetwork(RunSetting setting) : RunBase(setting)
    {
        private IReadOnlyList<HostIP>? RemoteHostList;

        protected override Task ReportTestResultAsync()
        {
            ConsoleHelper.WriteLine($"正在将测试结果发送到服务器···");

            if (TestResultList is not { Count: > 0 })
                return Task.CompletedTask;

            return ApiClient.Instance.ReportListAsync(TestResultList);
        }

        protected override async Task<IReadOnlyList<HostIP>> CreatHostIpListAsync()
        {
            var list = await base.CreatHostIpListAsync();
            return list.Concat(RemoteHostList ?? Array.Empty<HostIP>())
                       .Distinct(HostIPDistinctCompare.Instance)
                       .ToArray();
        }

        protected override async Task<IReadOnlyList<string>> CreatHostListAsync()
        {
            RemoteHostList = await ApiClient.Instance.GetHostIPListAsync(Setting.Path);
            return RemoteHostList.Select(h => h.Host).Distinct().ToArray();
        }
    }
}
